package nhatquang.gs.model;

import android.content.Context;
import android.content.SharedPreferences;

public class UserData {
	private static final String PREFS_NAME = "GAME_USERDATA_GOLDSOFT";
	private static final String UNLOCKED_MAX_LEVEL_KEY = "unlockedMaxLevels";
	private static final String SOUND_KEY = "soundKey";
	private static final String[] LEVEL = { "level1", "level2", "level3",
			"level4", "level5", "level6", "level7", "level8", "level9",
			"level10", "level11", "level12", "level13", "level14", "level15",
			"level16", "level17", "level18" };
	private SharedPreferences mSettings;
	private SharedPreferences.Editor mEditor;
	private int mUnlockedMaxLevels;
	private boolean mSoundEnabled;
	private int mUnlockedLevels;

	public synchronized void init(Context pContext) {
		if (mSettings == null) {
			mSettings = pContext.getSharedPreferences(PREFS_NAME,
					Context.MODE_PRIVATE);
			mEditor = mSettings.edit();

			mUnlockedMaxLevels = mSettings.getInt(UNLOCKED_MAX_LEVEL_KEY, 1);

			mSoundEnabled = mSettings.getBoolean(SOUND_KEY, true);
		}
	}

	/* retrieve the max unlocked level value */
	public synchronized int getMaxUnlockedLevel() {
		return mUnlockedMaxLevels;
	}

	public synchronized void unlockNextLevel(int rate) {
		// Increase the max level by 1
		mUnlockedLevels++;
		/*
		 * Edit our shared preferences unlockedLevels key, setting its value our
		 * new mUnlockedLevels value
		 */
		mEditor.putInt(UNLOCKED_MAX_LEVEL_KEY, mUnlockedLevels);
		/*
		 * commit() must be called by the editor in order to save changes made
		 * to the shared preference data
		 */
		mEditor.commit();
	}

	public synchronized int getLevel(int level) {
		return mSettings.getInt(LEVEL[level], -1);

	}

	public synchronized void setLevel(String level, int rate) {
		mEditor.putInt(level, rate);
	}

	public SharedPreferences getmSettings() {
		return mSettings;
	}

	public void setmSettings(SharedPreferences mSettings) {
		this.mSettings = mSettings;
	}

	public SharedPreferences.Editor getmEditor() {
		return mEditor;
	}

	public void setmEditor(SharedPreferences.Editor mEditor) {
		this.mEditor = mEditor;
	}

	public int getmUnlockedLevels() {
		return mUnlockedLevels;
	}

	public void setmUnlockedLevels(int mUnlockedLevels) {
		this.mUnlockedLevels = mUnlockedLevels;
	}

	public boolean ismSoundEnabled() {
		return mSoundEnabled;
	}

	public void setmSoundEnabled(boolean mSoundEnabled) {
		this.mSoundEnabled = mSoundEnabled;
	}

	public static String getPrefsName() {
		return PREFS_NAME;
	}

	public static String getUnlockedLevelKey() {
		return UNLOCKED_MAX_LEVEL_KEY;
	}

	public static String getSoundKey() {
		return SOUND_KEY;
	}

}
